#include "equalizer.h"
#include "ui_equalizer.h"
#include <QtGui>
#define padding 40
Equalizer::Equalizer(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::Equalizer)
{

    ui->setupUi(this);
    this->show();
    unsigned hz1 = 55, hz2 = 77, hz;
    //this->setWidget(ui->horizontalLayoutWidget);
    for (int i = 0; i < EQS_MAX; i++)
    {
        if (i == 0)
            hz = hz1;
        if (i == 1)
            hz = hz2;
        if (i > 1)
            if (i % 2 == 0)
                hz = hz1 *= 2;
            else
                hz = hz2 *= 2;
        eqs[i] = new QTS(hz);
        ui->lbl_bottom_layout->addWidget(eqs[i]->lbl_down);
        ui->lbl_top_layout->addWidget(eqs[i]->lbl_top);
        ui->sl_layout->addWidget(eqs[i]->sl);

    }
    this->setWindowTitle(tr("Equalizer"));
    QObject::connect(ui->app_btn, SIGNAL(clicked()), this, SIGNAL(update_channels()));
}



QSlider * QTS::qsl_create(void)
{

    QSlider *res = new QSlider(Qt::Vertical);
    res->setMaximum(MAX_DB);
    res->setMinimum(MIN_DB);
    res->setTickPosition(QSlider::TicksBothSides);
    res->setGeometry(sl_rect);
    return res;
}

QLabel * QTS::qlbl_create(QString &text)
{
    QLabel *res = new QLabel(text);
    return res;
}


Equalizer::~Equalizer()
{
    delete ui;
}

QTS::QTS(unsigned n_hz)
{
    this->hz = n_hz;
    sl_rect.setX(-padding);
    sl_rect.setY(10);
    sl_rect.setWidth(60);
    sl_rect.setHeight(231);
    this->sl = qsl_create();
    this->val = 0;
    this->sl->setValue(0);
    QString txt;
    if (this->hz < 1000)
        txt =  QString::number(this->hz) + tr("Hz");
    else
        txt = QString::number(this->hz / 1000.0, 'g', 2) + tr("KHz");
    this->lbl_down = qlbl_create(txt);

    txt = QString::number(this->sl->value());
    this->lbl_top = qlbl_create(txt);
    //QTS
    QObject::connect(this->sl, SIGNAL(sliderMoved(int)), this, SLOT(update_ref(void)));
    QObject::connect(this->sl, SIGNAL(sliderReleased()), this, SLOT(update_ref(void)));
}
